package com.shujia.homework1;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

public class MyReducer extends Reducer<Text, Text,Text,Text> {
    @Override
    protected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context) throws IOException, InterruptedException {
        ArrayList<String> list = new ArrayList<>();
        String city = null;
        for (Text value : values) {
            String str = value.toString();
            if (str.startsWith("$")) {
                String s = str.substring(1);
                String[] split = s.split("-");
                city = split[1];
            } else if (str.startsWith("#")) {

                list.add(str.substring(1));
            }
        }
        for (String s :list){
            String[] tmp = s.split(",");
            context.write(new Text(tmp[0]),new Text(tmp[1]+","+city+","+tmp[2]));
        }
    }
}
